<template>
  <div class="edit">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>{{title}}</span>
        <el-button @click="$router.go(-1)" style="float: right; padding: 3px 0" type="text">返回</el-button>
      </div>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="文章标题" prop="newsName">
          <el-radio-group v-model="form.newsType">
            <el-radio label="首页">首页</el-radio>
            <el-radio label="探索">探索</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="文章标题" prop="newsName">
          <el-input v-model="form.newsName" placeholder="请输入文章标题" />
        </el-form-item>
        <el-form-item label="封面图" prop="newsCover">
          <Upload v-model="form.newsCover" @clear="clearImageValidate" />
        </el-form-item>
        <el-form-item label="点击量" prop="newsDesc">
          <el-input type="number" v-model="form.lookNum"></el-input>
        </el-form-item>
        <el-form-item label="简介" prop="newsDesc">
          <el-input type="textarea" v-model="form.newsDesc"></el-input>
        </el-form-item>
        <el-form-item label="内容详情" prop="newsContent">
          <Editor v-model="form.newsContent" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">确定</el-button>
          <el-button @click="cancel">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
<script>
import Editor from "@/components/Editor";
import Upload from "@/components/Upload";
import { getNews, addNews, updateNews } from "@/api/system/news";

export default {
  name: "NewsEdit",
  components: {
    Editor,
    Upload
  },
  data() {
    return {
      title: "添加资讯",
      // 表单参数
      form: {
        newsName: "",
        newsCover: "",
        newsDesc: "",
        newsContent: ""
      },
      // 表单校验
      rules: {
        newsName: [
          { required: true, message: "请输入文章标题", trigger: "blur" }
        ],
        newsCover: [{ required: true, message: "请上传封面图" }],
        newsDesc: [{ required: true, message: "请输入简介", trigger: "blur" }],
        newsContent: [
          { required: true, message: "请输入内容详情", trigger: "blur" }
        ]
      }
    };
  },
  mounted() {
    this.getEditInfo();
  },
  methods: {
    clearImageValidate() {
      this.rules.newsCover = [];
      this.$refs["form"].clearValidate("newsCover");
    },
    getEditInfo() {
      const id = this.$route.query.id;
      if (id) {
        getNews(id).then(response => {
          this.form = response.data;
        });
        this.title = "修改资讯";
      }
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          const postData = {
            ...this.form,
            newsContent: this.filterContent(this.form.newsContent)
          };

          if (this.form.id != null) {
            updateNews(postData).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.$router.go(-1);
              }
            });
          } else {
            addNews(postData).then(response => {
              if (response.code === 200) {
                this.msgSuccess("添加成功");
                this.$router.go(-1);
              }
            });
          }
        }
      });
    },
    // 重置按钮
    cancel() {
      this.getEditInfo();
      this.resetForm("form");
    }
  }
};
</script>
